﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;

namespace K9Nano.AspNetCore.Authorization
{
    public class K9AuthorizationPolicyProvider : IAuthorizationPolicyProvider
    {
        public DefaultAuthorizationPolicyProvider FallbackPolicyProvider { get; }

        public K9AuthorizationPolicyProvider(IOptions<AuthorizationOptions> options)
        {
            FallbackPolicyProvider = new DefaultAuthorizationPolicyProvider(options);
        }

        public Task<AuthorizationPolicy> GetDefaultPolicyAsync()
        {
            return K9RbacRequirement.RbacPolicyTask;
        }

        public async Task<AuthorizationPolicy> GetFallbackPolicyAsync()
        {
            var policy = await FallbackPolicyProvider.GetFallbackPolicyAsync();
            return policy;
        }

        public Task<AuthorizationPolicy> GetPolicyAsync(string policyName)
        {
            return FallbackPolicyProvider.GetPolicyAsync(policyName);
        }
    }
}